<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//会员管理
class User extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('User/UserModel','CurModel');
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		
		$filter = array('username','name','reg_time','company','bumen','region');
		$arr = array(
			'filter'=>$filter
		);
		
		$result = parent::_get_list($arr);
		if(!$result){
			$this->err_out($this->getError());
		}

		
		$this->data_out($result);
	}
	//start
	/**
	* @name 获取全部
	* @return 成功返回1
	*/
	//end
	function get_all(){
		
		$list = $this->CurModel->getAll(array('is_enable'=>1),"*");
		
		foreach($list as $key=>$val){
			$list[$key] = $this->info_out($val);
		}
		
		$this->data_out($list);
	}
	
	function info_out($info){
		$info['last_login'] = date('Y-m-d H:i',$info['last_login']);
		$info['addtime'] = date('Y-m-d H:i',$info['addtime']);
		
		$info = $this->data_filter($info,null,array('password'));
		
		return $info;
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function get_info(){
		$id = $this->input->post('id',null,0);
		
		$result = parent::_get_info($id);
		if(!$result){
			$this->err_out($this->getError());
		}

		//通过该会员ID获取权限组相关信息，ling添加
		$this->load->dmodel('priv_group','priv_group');
		$res = $this->priv_group->getAll(array('is_enable'=>1));
		$privs_name = array();
		foreach($res as $val){
			$ls_ids = explode(',',$val['ids']);
			if(in_array($id,$ls_ids)){
				
				$privs_name[] = $val['name'];
				
			}
		}
		if(!empty($privs_name)){
			$result['group_name'] = implode(',',$privs_name);
		}else{
			$result['group_name'] = '暂无权限组';
		}
		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function save(){

		if(!$this->check_priv('user_update|user_add')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$filter = array();
		$post = array('nickname','headimg','sex','birthday','info','mobile');
		$farr = array(
			'post'=>$post,
			'filter'=>$filter
		);
		
		$result = parent::_save($farr);
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->add_log("保存用户【{$result['id']}】信息",'user');
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 密码变更
	* @param id ture int ID
	* @param password ture char 密码
	* @param confirm_password ture char 确认密码
	* @return 成功返回1
	*/
	//end
	function change_password(){
		
		if(!$this->check_priv('user_update')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$id = $this->input->post('id',null,0);
		$password = $this->input->post('password',null,'');
		$confirm_password = $this->input->post('confirm_password',null,'');
		if($password===''){
			$this->err_out('密码不能为空');
		}
		if(mb_strlen($password)<6 || mb_strlen($password)>18){
			$this->err_out('请输入位数在6至18之间的密码');
		}
		
		if($password!==$confirm_password){
			$this->err_out('两次密码输入不一样');
		}
		$pwd = md5($password);
		$this->CurModel->edit(array('password'=>$pwd),"id='{$id}'");
		
		$this->add_log("修改用户【{$id}】密码",'user');
		
		$this->data_out(1);
	}
	
	//start
	/**
	* @name 添加会员
	* @param id ture int ID
	* @param password ture char 密码
	* @param confirm_password ture char 确认密码
	* @return 成功返回1
	*/
	//end
	function add(){
		
		if(!$this->check_priv('user_add')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$password = $this->input->post('password',null,'');
		$confirm_password = $this->input->post('confirm_password',null,'');
		if($password===''){
			$this->err_out('密码不能为空');
		}
		if(mb_strlen($password)<6 || mb_strlen($password)>18){
			$this->err_out('请输入位数在6至18之间的密码');
		}
		
		if($password!==$confirm_password){
			$this->err_out('两次密码输入不一样');
		}
		
		$pwd = md5($password);
		
		
		$time = time();
		
		$post = $this->input->post();
		
		$farr = array('username','nickname','en_name','sex','company','bumen','region','rank','mobile','email','phone');
		$arr = array();
		foreach($post as $key=>$val){
			if(in_array($key,$farr)){
				$arr[$key] = $val;
			}
		}
		
		
		$data = array(
			'username'=>'员工编号不能为空',
			'nickname'=>'中文名称不能为空',
			'en_name'=>'英文名称不能为空',
			'company'=>'请选择合同单位',
			'bumen'=>'请选择部门',
			'region'=>'地区不能为空',
		);
		$result = $this->CheckData($data,$arr);
		if($result!==false){
			$this->err_out($result);
		}
		$result = $this->CurModel->_where_exist(array('username'=>$arr['username']));
		if($result){
			$this->err_out('不能设置重复的账号');
		}
		$result = $this->CurModel->_where_exits(array('mobile'=>$arr['mobile']));
		if($result){
			//$this->err_out('该手机号码已经绑定');
		}
		
		$arr['addtime'] = $time;
		$arr['password'] = $pwd;
		$id = $this->CurModel->add($arr);
		
		$this->add_log("添加用户【{$id}】",'user');
		
		$this->data_out(1);
	}
	
	//start
	/**
	* @name 修改会员
	* @param id ture int ID
	* @param password ture char 密码
	* @param confirm_password ture char 确认密码
	* @return 成功返回1
	*/
	//end
	function update(){
		
		if(!$this->check_priv('user_edit')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$id = $this->input->post('id',null,0);
		
		$info = $this->CurModel->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$password = $this->input->post('password',null,'');
		$confirm_password = $this->input->post('confirm_password',null,'');
		
		$time = time();
		
		$post = $this->input->post();
		
		$farr = array('nickname','en_name','sex','company','bumen','region','rank','mobile','email','phone');
		$arr = array();
		foreach($post as $key=>$val){
			if(in_array($key,$farr)){
				$arr[$key] = $val;
			}
		}
		
		
		$data = array(
			'nickname'=>'中文名称不能为空',
			'en_name'=>'英文名称不能为空',
			'company'=>'请选择合同单位',
			'bumen'=>'请选择部门',
			'region'=>'地区不能为空',
		);
		$result = $this->CheckData($data,$arr);
		if($result!==false){
			$this->err_out($result);
		}
		
		if(!empty($password)){
			if(mb_strlen($password)<6 || mb_strlen($password)>18){
				$this->err_out('请输入位数在6至18之间的密码');
			}
			
			if($password!==$confirm_password){
				$this->err_out('两次密码输入不一样');
			}
			
			$pwd = md5($password);
			$arr['password'] = $pwd;
		}
		
		
		
		$id = $this->CurModel->edit($arr,"id='{$id}'");
		
		$this->add_log("修改用户【{$id}】信息",'user');
		
		$this->data_out(1);
	}
	//start
	/**
	* @name 删除
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function del(){
		
		if(!$this->check_priv('user_del')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$result = parent::_del();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		
		$this->add_log("删除用户【{$result}】",'user');
		$this->data_out($result);
		
	}
	
	//start
	/**
	* @name 获取会员统计
	* @return 成功返回1
	*/
	//end
	function get_counts(){
		$data = array();
		
		$count = $this->CurModel->getCount();
		$data['count'] = $count;
		
		$now_day = date('Y-m-d');
		$day_count = $this->CurModel->getCount("FROM_UNIXTIME(addtime, '%Y-%m-%d')='{$now_day}'");
		$data['day_count'] = $day_count;
		
		$now_month = date('Y-m');
		$month_count = $this->CurModel->getCount("FROM_UNIXTIME(addtime, '%Y-%m')='{$now_month}'");
		$data['month_count'] = $month_count;
		
		$this->data_out($data);
	}
	//start
	/**
	* @name 批量操作
	* @param ids ture int_array ID组合
	* @param act ture int 操作类型enable，disconnected，delete
	* @return 成功返回1
	*/
	//end
	function batch_act(){
		$ids = $this->input->post('ids',null,array());
		$act = $this->input->post('act',null,'');
		
		$act_arr = array('enable','disconnected','delete');
		if(!in_array($act,$act_arr)){
			$this->err_out('错误的错误');
		}
		if(!is_array($ids)){
			$ids = explode(',',$ids);
		}
		if(count($ids)<=0){
			$this->err_out('请选择成员');
		}
		$ls_ids = array();
		foreach($ids as $id){
			if(!empty($id)){
				$ls_ids[] = $id;
			}
		}
		
		if($act=='enable'){
			$this->CurModel->edit(array('is_enable'=>1),"id in(".implode(',',$ls_ids).")");
		}
		elseif($act=='disconnected'){
			$this->CurModel->edit(array('is_enable'=>0),"id in(".implode(',',$ls_ids).")");
		}
		elseif($act=='delete'){
			$this->CurModel->del("id in(".implode(',',$ls_ids).")");
		}
		$this->data_out(1);
	}
	//start
	/**
	* @name 设置是否启用
	* @param id ture int ID
	* @param status ture int 状态
	* @return 成功返回1
	*/
	//end
	function set_enable(){
		$id = $this->input->post('id',null,0);
		$status = $this->input->post('status',null,0);
		$info = $this->CurModel->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$this->CurModel->edit(array('is_enable'=>$status),"id ='{$id}'");
		
		$this->data_out(1);
	}
	
	//start
	/**
	* @name 获取登录日志
	* @param id ture int ID
	* @param status ture int 状态
	* @return 成功返回1
	*/
	//end
	function get_log_list(){
		$user_id = $this->user_id;
		
		$uid = $this->input->post('uid',null,0);
		$page = $this->input->post('page',null,1);
		$size = $this->input->post('size',null,10);
		
		
		$this->load->model('User/LoginLogModel','LoginLogModel');
		$res = $this->LoginLogModel->get_list(array('user_id'=>$uid),$page,$size);
		$list = $res['list'];
		foreach($list as $key=>$val){
			$list[$key]['addtime'] = date('Y-m-d H:i:s',$val['addtime']);
			
		}
		$res['list'] = $list;
		$this->data_out($res);
	}
}
